package com.twotiger.and.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import app.twotiger.p2p.R;

import com.twotiger.and.activity.account.invested.InverstedRecordDetailPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.bean.Invest;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;

/**
 * 已投项目的adapter
 * Created by victor on 2014/11/7 0007.
 */
public class DealRecordAdapter extends BaseAdapter {
    private BaseActivity mactivity;
    private ArrayList<Invest> mList;
    private String isCyOrYh; //持有中还是已还款

    public DealRecordAdapter(Activity _Activity, ArrayList<Invest> _List) {
        this.mactivity = (BaseActivity) _Activity;
        this.mList = _List;
    }

    public void addData(List<Invest> invests, String isCyOrYh) {
        if (!ListUtils.isEmpty(invests)) {
            mList.addAll(invests);
        }
        notifyDataSetChanged();
        this.isCyOrYh = isCyOrYh;
    }

    public void resetData(List<Invest> invests, String isCyOrYh) {
        mList.clear();
        if (!ListUtils.isEmpty(invests)) {
            mList.addAll(invests);
        }
        notifyDataSetChanged();
        this.isCyOrYh = isCyOrYh;
    }

    @Override
    public int getCount() {
        if (!ListUtils.isEmpty(mList)) {
            return mList.size();
        } else {
            return 1;
        }
    }

    @Override
    public Invest getItem(int position) {
        if (!ListUtils.isEmpty(mList) && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Invest invest = getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mactivity).inflate(R.layout.deal_record_item, null);
        }
        final TextView recorde_date_text = ViewHolder.get(convertView, R.id.recorde_date_text);
        final TextView project_code = ViewHolder.get(convertView, R.id.project_code);
        final TextView project_recycle_value = ViewHolder.get(convertView,
            R.id.project_recycle_value);
        final TextView inverst_money = ViewHolder.get(convertView, R.id.inverst_money);

        if (invest != null) {
            final String stringForLong = DateUtil.getDateStringForLong(
                Long.valueOf(invest.getCtime()), DateUtil.DATE_FMT);
            recorde_date_text.setText(stringForLong);
            project_code.setText(invest.getProjectName());
            project_recycle_value.setText(invest.getProjectPeriod() + "");
            inverst_money.setText(ArithUtils.coverMoneyComma(invest.getAmount())); //invest.getAmount()

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mactivity, InverstedRecordDetailPage.class)
                        .putExtra("id", invest.getInvestId()).putExtra("type", invest.getType())
                        .putExtra("isCyOrYh", isCyOrYh).putExtra("mode", invest.getMode());
                    mactivity.startNewActivity(intent, R.anim.push_left_in, R.anim.push_left_out,
                        false);
                }
            });
        }
        return convertView;
    }
}
